package com.jxw.launcher;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import com.jxw.constant.Constant;
import com.jxw.studyrecordprovider.util.StudyRecorderUtil;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class CrashLogUtils {
    private static final String LOG_DIR_PATH = Environment.getExternalStorageDirectory().toString() + "/zzj_log/";

    public static void postCrashLog(Context context, Throwable th) throws JSONException, UnsupportedEncodingException {
        String crashReport = getCrashReport(context, th);
        String format = new SimpleDateFormat(StudyRecorderUtil.DEFAULT_FORMAT).format(new Date());
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("androidVersion", Build.VERSION.RELEASE);
        jSONObject.put("androidBuild", Build.DISPLAY);
        jSONObject.put("versionName", context.getPackageName());
        jSONObject.put("versionCode", getVersion(context));
        jSONObject.put("crashReportStr", crashReport);
        jSONObject.put(Constant.DATE, format);
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://alunchering/AndroidCrashCollector/crashlog");
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(jSONObject.toString(), "UTF-8"));
        try {
            EntityUtils.toString(defaultHttpClient.execute(httpPost).getEntity());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

    public static void saveLogToFile(Context context, Throwable th) throws JSONException, UnsupportedEncodingException {
        if (Environment.getExternalStorageState().equals("mounted")) {
            File file = new File(LOG_DIR_PATH);
            if (!file.exists()) {
                file.mkdirs();
            }
            String crashReport = getCrashReport(context, th);
            try {
                FileWriter fileWriter = new FileWriter(LOG_DIR_PATH + getAppInfo(context) + timeStr() + ".txt");
                fileWriter.write(crashReport);
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String getCrashReport(Context context, Throwable th) {
        StringBuffer stringBuffer = new StringBuffer();
        StringWriter stringWriter = new StringWriter();
        th.printStackTrace(new PrintWriter(stringWriter));
        stringBuffer.append(stringWriter.toString());
        return stringBuffer.toString();
    }

    public static String getVersion(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static String getAppInfo(Context context) {
        try {
            String packageName = context.getPackageName();
            String str = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
            return packageName + "_" + str;
        } catch (Exception unused) {
            return null;
        }
    }

    public static String timeStr() {
        return new SimpleDateFormat("yyyy-MM-dd_HH-MM-SS").format(new Date());
    }
}
